<?php
namespace app\web\controller\admin;

use app\admin\library\AdminController;
use app\web\library\PartPower;

class Part extends AdminController{

    public function __construct()
    {
        parent::__construct();

    }

    public function onIndex(){
        $this->power('web.part');
        $this->pageTitle = '栏目管理';
        $this->pageIcon = 'fa-navicon';
        return $this->display();
    }

    public function onTree(){
        $this->power('web.part');

        $part = $this->model('Part');
        $power = $this->post('power','part');
        
        
        $list = $part->getTreeData(0);

        $partPower = new PartPower($this->app);
        
        switch($power){
            case 'part':
                if (!$partPower->partSuper()){
                    $powerList = $partPower->getPartList();
                    $list = $this->delUnPowerNode($list,$powerList);
                }
            break;
            case 'article':
                if (!$partPower->articlePartSuper()){
                    $powerList = $partPower->getArticlePartList();
                    $list = $this->delUnPowerNode($list,$powerList);
                }
            break;
            default:
                throw new \Exception('参数错误');
            break;
        }

        return ['tree' => $list];

    }

    public function onEdit(){
        $this->power('web.part');
        
        $id = $this->get('id',$this->check('ID')->type('int'));
    
        $this->pageTitle = '添加栏目';

        if ($id){
            $this->pageTitle = '编辑栏目';
            $part = $this->model('Part');
            $data = $part->getId($id);

            try{
                $param = json_decode($data['param'],true);
            }catch(\Exception $e){
                $param = [];
            }
            
            $partPower = new PartPower($this->app);
            if (!$partPower->inPart($data['part_id'])){
                throw new \Exception('您没有权限权限此栏目');
            }


            $this->set('part',$data);
            $this->set('param',$param);
        }

        return $this->display();
    }

    public function onRoute(){
        $part_id = $this->post('part',$this->check('ID')->type('int')->required());

        
        $part = $this->model('Part');

        return ['route' => $part->getRoute($part_id)];
    }

    public function onSave(){
        $this->power('web.part');
        
        $part_id = $this->post('part_id',$this->check('ID')->type('int'));

        $data['title'] =  $this->post('title',$this->check('title')->type('string')->required());
        $data['parent_id'] =  $this->post('parent_id',$this->check()->type('int')->required());
        $data['type'] = $this->post('type',$this->check()->type('string')->required());
        $data['route'] = $this->post('route',$this->check()->type('string'));
        $data['route_type'] = $this->post('route_type',$this->check()->type('string'));
        $data['brief'] = $this->post('brief',$this->check()->type('string'));
        $data['content'] = $this->post('content',$this->check()->type('string'));
        $data['part_model_id'] = $this->post('part_model_id',$this->check()->type('int'));
        $data['article_model_id'] = $this->post('article_model_id',$this->check()->type('int'));
        $data['part_template'] = $this->post('part_template',$this->check()->type('string'));
        $data['article_template'] = $this->post('article_template',$this->check()->type('string'));


        $partPower = new PartPower($this->app);
        

        if ($data['route_type']!='default'){
            if ($data['route']==''){
                throw new \Exception('路由不能为空');
            }
        }

        $data['param'] = json_encode($this->post('param',[]));
        
        $part = $this->model('Part');
        
        if ($part_id){

            if (!$partPower->inArticlePart($part_id)){
                throw new \Exception('您没有权限编辑此栏目');
            }

            $oldData = $part->getId($part_id);

            $childrenList = $part->getKeyCollect($oldData['part_id']);

            if (in_array($data['parent_id'],$childrenList)){
                throw new \Exception('不能选择子栏目作为父栏目');
            }


            if ($oldData['parent_id'] != $data['parent_id']){
                if (!$partPower->inArticlePart($data['parent_id'])){
                    throw new \Exception('您没有权限编辑此栏目');
                }
            }
            
            if ($part->updateId($part_id,$data)){
                $this->log('编辑栏目', ['part_id'=>$part_id,'title'=>$data['title']]);
                return true;
            }else{
                throw new \Exception('编辑栏目失败');
            }

        }else{

            if (!$partPower->inArticlePart($data['parent_id'])){
                throw new \Exception('您没有权限编辑此栏目');
            }

            $data['sort'] =  $part->getTreeChildrenCount($data['parent_id']);
            
            if ($part_id = $part->insert($data)){

                $this->log('添加栏目', ['part_id'=>$part_id,'title'=>$data['title']]);
                return true;
            }else{
                throw new \Exception('添加栏目失败');
            }
        }
    }

    public function onDel(){
        $this->power('web.part');
        
        $id = $this->post('part_id',$this->check('ID')->type('int')->required());

        $partPower = new PartPower($this->app);
        if (!$partPower->inPart($id)){
            throw new \Exception('不能删除当前栏目');
        }else{
            $part = $this->app->model('Part');
            $part->deleteId($id);
            return true;
        }

    }

    public function onSort(){
        $value = $this->post('value',$this->check('排序数据')->type('array')->required());

        $part = $this->model('Part');

        $part->switchSort($value,'sort','asc');
        return true;
    }


    private function delUnPowerNode($list,$powerList){
        $re = [];
        foreach ($list as $key => $value) {
            if (in_array($value['part_id'],$powerList)){
                
                if ($value['children'] && count($value['children'])>0){
                    $value['children'] =  $this->delUnPowerNode($value['children'],$powerList);
                }

                $re[]= $value;
            }
        }

        return $re;
    }




}